package com.gpframework.module.controller.system;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.system.domain.SmsSendLog;
import com.gpframework.module.system.domain.req.SmsSendLogAdd;
import com.gpframework.module.system.domain.req.SmsSendLogReq;
import com.gpframework.module.system.domain.req.SmsSendLogUpdate;
import com.gpframework.module.system.domain.vo.SmsSendLogVo;
import com.gpframework.module.system.service.ISmsSendLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * gpframework
 * ==================================================================
 * CopyRight © 2017-2099 广州宏略信息科技有限公司
 * 官网地址：http://www.greatplan.com.cn
 * <p>
 * ------------------------------------------------------------------
 * 这不是一个自由软件！未经本公司授权您只能在不用于商业目的的前提下
 * 对本程序代码进行修改和使用；不允许对本程序代码以任何目的的再发布。
 * ==================================================================
 *
 * @ClassName SmsSendLogController
 * @Author gpframework
 * @Date 2020-12-29
 * @Version 1.0.0
 * @Description 短信发送记录Controller
 */
@Validated
@Api(tags = "短信发送记录")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes", "unchecked"})
@RequestMapping("/system/smsSendLog")
public class SmsSendLogController extends BaseController {

    private final ISmsSendLogService iSmsSendLogService;

    /**
     * @Description 分页
     * @Author gpframework
     * @Date 2020-12-29
     * @Param smsSendLog
     * @Return TableDataInfo
     */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('system:smsSendLog:list')")
    @GetMapping("/list")
    public TableDataInfo<SmsSendLogVo> list(SmsSendLogReq smsSendLogReq) {
        startPage();
        QueryWrapper<SmsSendLog> lqw = new QueryWrapper<SmsSendLog>();
        lqw.orderByDesc("gp.update_time");
        smsSendLogReq.generatorQuery(lqw, true);
        List<SmsSendLogVo> list = iSmsSendLogService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2020-12-29
     * @Param smsSendLog
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('system:smsSendLog:add')")
    @Log(title = "添加短信发送记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody SmsSendLogAdd smsSendLogAdd) {
        SmsSendLog smsSendLog = Convert.convert(new TypeReference<SmsSendLog>() {
        }, smsSendLogAdd);
        return toAjax(iSmsSendLogService.save(smsSendLog) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2020-12-29
     * @Param smsSendLog
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('system:smsSendLog:edit')")
    @Log(title = "修改短信发送记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody SmsSendLogUpdate smsSendLogUpdate) {
        SmsSendLog smsSendLog = Convert.convert(new TypeReference<SmsSendLog>() {
        }, smsSendLogUpdate);
        return toAjax(iSmsSendLogService.updateById(smsSendLog) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2020-12-29
     * @Param smsSendLog
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('system:smsSendLog:export')")
    @Log(title = "导出短信发送记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SmsSendLogReq smsSendLogReq) {
        QueryWrapper<SmsSendLog> lqw = new QueryWrapper<SmsSendLog>();
        smsSendLogReq.generatorQuery(lqw, true);
        List<SmsSendLogVo> list = iSmsSendLogService.listVo(lqw);
        ExcelUtil<SmsSendLogVo> util = new ExcelUtil<SmsSendLogVo>(SmsSendLogVo.class);
        return util.exportExcel(list, "smsSendLog");
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2020-12-29
     * @Param smsSendLog
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('system:smsSendLog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult<SmsSendLogVo> getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iSmsSendLogService.getVoById(id));
    }


    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2020-12-29
     * @Param smsSendLog
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('system:smsSendLog:remove')")
    @Log(title = "删除短信发送记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iSmsSendLogService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
